package com.heba.helper;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;

import com.heba.Debug;
import com.heba.greenmulu.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期转换类
 * <p>
 * Created by shenghao on 2015/6/15.
 */
public class DateHelper {

    public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat();

    public static Date getUTCByLocal(Date localDate) {
        java.util.Calendar localCalendar = java.util.Calendar.getInstance();
        localCalendar.setTime(localDate);
        int zoneOffset = localCalendar.get(java.util.Calendar.ZONE_OFFSET);
        int dstOffset = localCalendar.get(java.util.Calendar.DST_OFFSET);
        localCalendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        Date utcDate = new Date(localCalendar.getTimeInMillis());
        return utcDate;
    }

    public static Date getLocalByUTC(Date utcDate) {
        java.util.Calendar localCalendar = java.util.Calendar.getInstance();
        localCalendar.setTime(utcDate);
        int zoneOffset = localCalendar.get(java.util.Calendar.ZONE_OFFSET);
        int dstOffset = localCalendar.get(java.util.Calendar.DST_OFFSET);
        localCalendar.add(java.util.Calendar.MILLISECOND, (zoneOffset + dstOffset));
        Date localDate = new Date(localCalendar.getTimeInMillis());
        return localDate;
    }

    public static Date String2UTCDate(String time) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date utcDate = getUTCByLocal(format.parse(time));
            return utcDate;
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }

    public static Date UTCDate2LocalDate(Date date, String format) {
        String time = formatDateUTC2Local(date, format);
        Date localDate = parseDate(time, format);
        return localDate;
    }

    public static String formatDateUTC2Local(Date date, String format) {
        return formatDate(getLocalByUTC(date), format);
    }

    //时间格式化显示 1991-09-11 12:00:00
    public static String formatDateUTC2LocalDateTime(Date date) {
        return formatDate(getLocalByUTC(date), "yyyy-MM-dd HH:mm:ss");
    }

    //时间格式化显示 1991-09-11
    public static String formatDateUTC2LocalDate(Date date) {
        return formatDate(getLocalByUTC(date), "yyyy-MM-dd");
    }

    //时间格式化显示星期几 1991-09-11 sun
    public static String formatDateUTC2LocalWeek(Date date) {
        return formatDate(getLocalByUTC(date), "yyyy-MM-dd EEEE");
    }

    /**
     * 字符串时间格式转换成date格式
     *
     * @param dateChar
     * @param format
     * @return
     */
    public static Date parseDate(String dateChar, String format) {
        simpleDateFormat.applyPattern(format);
        try {
            return simpleDateFormat.parse(dateChar);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }

    /**
     * 字符串时间格式转换成date格式
     *
     * @param dateChar
     * @return
     */
    public static Date parseDate(String dateChar) {
        return parseDate(dateChar, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Date转换成字符串格式
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        simpleDateFormat.applyPattern(format);
        return simpleDateFormat.format(date);
    }

    /**
     * Date转换成字符串格式
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @param UTCDate 开始时间  UTCDate
     * @return
     */
    public static String formatDate1(Date UTCDate) {
        try {
            UTCDate = getLocalByUTC(UTCDate);
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            String localDate = format1.format(UTCDate);
            Date utcDate = getUTCByLocal(format1.parse(localDate));
            return formatDate(utcDate);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "";
    }

    /**
     * 獲取系統時間格式 （日期）yyyy-MM-dd
     *
     * @param contentResolver
     * @return
     */
    public static String getSystemFormat(ContentResolver contentResolver) {
        // 获取当前系统设置
        String strTimeFormat = Settings.System.getString(contentResolver,
                Settings.System.DATE_FORMAT);
//      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strTimeFormat);
        Debug.Log("SystemDateFormat", strTimeFormat);
        return strTimeFormat;
    }

    /**
     * 转换日期格式
     *
     * @param datetime
     * @param format
     * @return
     * @throws ParseException
     */
    public static String transferFormat(String datetime, String format) throws ParseException {
        SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat s2 = new SimpleDateFormat(format);
        Date tempDate = s1.parse(datetime);
        String outTime = s2.format(tempDate);
        return outTime;
    }

    /**
     * 转换成APP要求显示的时间格式
     *
     * @param context
     * @param dateTime
     * @return
     */
    public static String transferDateString(Context context, String dateTime) {
        String dateStr = "";
        try {
            if (isToday(dateTime)) {
                dateStr = context.getResources().getString(R.string.common_date_today) + " " +
                        transferFormat(dateTime, context.getResources().getString(R.string.common_date_format1));
            } else if (isYesterday(dateTime)) {
                dateStr = context.getResources().getString(R.string.common_date_yesterday) + " " +
                        transferFormat(dateTime, context.getResources().getString(R.string.common_date_format1));
            } else {
                dateStr = transferFormat(dateTime, context.getResources().getString(R.string.common_date_format2));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2020-02-28 10:10:30" "2020-02-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param day 传入的 时间  "2020-02-28 10:10:30" "2020-02-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static boolean isYesterday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * getDateFormat
     *
     * @return
     */
    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }

    /**
     * DateLocal
     */
    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();


}
